package com.wfm.leetcode.editor.cn;

/**
 * 删除有序数组中的重复项
 * 2025-02-16 19:46:10
 * <p>
 * 快慢指针，利用相等的元素在数组中的下标一定是连续的
 * slow和fast都指向下标为1的元素开始，如果fast和前一个一样，则++
 * 时间复杂度O(n)，空间复杂度O(1)
 */
class RemoveDuplicatesFromSortedArray {

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int removeDuplicates(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new RemoveDuplicatesFromSortedArray().new Solution();
        
    }
}